package com.android.server.mock.accessibility;

import android.accessibilityservice.AccessibilityServiceInfo;
import android.animation.ValueAnimator;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.ResolveInfo;
import android.content.pm.ServiceInfo;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.mock.IMockManager;
import android.os.mock.MockManager;
import android.text.TextUtils;
import android.util.Log;
import android.util.Slog;

import com.android.server.mock.DataParser;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public final class MockAccessibilityImpl {
    private static String TAG = MockAccessibilityImpl.class.getSimpleName();

    private static MockAccessibilityImpl sDefaultMockAccessibility;
    private List<AccessibilityServiceInfo> installedAccessibilityInfoList = new ArrayList<>();


    private MockAccessibilityImpl() {
        initialize();
    }

    private static void initialize() {
        //TODO
    }

    public static MockAccessibilityImpl getInstance() {
        synchronized (MockAccessibilityImpl.class) {
            if (sDefaultMockAccessibility == null) {
                sDefaultMockAccessibility = new MockAccessibilityImpl();
            }
            return sDefaultMockAccessibility;
        }
    }

    public boolean parseInstalledAccessibilityServiceListInfo(Context context, JSONObject userDeviceInfo) {
        boolean result = true;
        try {
            JSONArray accessibilityInfoList = userDeviceInfo.getJSONArray("installedAccessibilityServiceList");
            if (accessibilityInfoList.length() > 0) {
                int length = accessibilityInfoList.length();
                for (int i = 0; i < length; i++) {
                    JSONObject item = (JSONObject) accessibilityInfoList.get(i);
                    if (item != null && item.has("className")) {
                        String flags = item.getString("flags");
                        String className = item.getString("className");
                        String labelName = item.getString("labelName");
                        String packageName = item.getString("packageName");
                        String permission = item.getString("permission");
                        String processName = item.getString("processName");

                        //Check if the data is valid.
                        if (TextUtils.isEmpty(className) || TextUtils.isEmpty(labelName)) {
                            continue;
                        }

                        try {
                            ServiceInfo serviceInfo = new ServiceInfo();
                            serviceInfo.name = className;
                            serviceInfo.splitName = "mock#" + labelName;
                            int flag = 0;
                            try {
                                flag = Integer.parseInt(flags);
                            } catch (Exception e) {}

                            serviceInfo.flags = flag;
                            serviceInfo.packageName = packageName;
                            serviceInfo.processName = processName;
                            serviceInfo.permission = permission;
                            serviceInfo.applicationInfo = new ApplicationInfo();

                            ResolveInfo resolveInfo = new ResolveInfo();
                            resolveInfo.serviceInfo = serviceInfo;
                            AccessibilityServiceInfo accessibilityServiceInfo = new AccessibilityServiceInfo(resolveInfo, context);
                            accessibilityServiceInfo.flags = flag;
                            accessibilityServiceInfo.setComponentName(new ComponentName(packageName,className));

                            installedAccessibilityInfoList.add(accessibilityServiceInfo);

                            Log.d(TAG, "parseInstalledAccessibilityServiceListInfo: " + serviceInfo);
                        } catch (Exception e) {
                            result = false;
                            Slog.e(TAG, "parseInstalledAccessibilityServiceListInfo:" + e.getMessage());
                        }
                    }
                }
            }
        } catch (Exception e) {
            result = false;
            Slog.e(TAG, "parseInstalledAccessibilityServiceListInfo:" + e.getMessage());
        }

        return result;
    }

    public List<AccessibilityServiceInfo> getInstalledAccessibilityServiceList() {
        return installedAccessibilityInfoList;
    }

    public List<AccessibilityServiceInfo> getEnabledAccessibilityServiceList() {
        //TODO
        return Collections.emptyList();
    }

    public void  cleanMockAccessibilityInfo() {
        installedAccessibilityInfoList.clear();
    }

    public boolean needMock(int userId) {
        return MockManager.getInstance().isPs(userId);
    }
}
